var pageSize = 20;
var pageStart = 1;
var title = "";
$(function(){
	pageStart = $("#page").val();
	title = $("#nav-search").val();
	if(title&&title!=''){
		loadArticle(title);
	}
});

function loadArticle(title){
	$.ajax({
		url:BASE_PATH+'/index/search.do',
		type:'post',
		dataType:'json',
		data:{
			pageSize:pageSize,
			pageStart:pageStart,
			title:title
		},
		success:function(data){
			createArticle(data);
			//一页
			if(data.pageCount<=1){
				$("#pageCount").html("");
				$("#currentPage").html("");
				$("#middle").html("");
				$("#prevPage").html("");
				$("#nextPage").html("");
			}else{
				$("#pageCount").html("共"+data.pageCount+"页");
				$("#middle").html("/");
				$("#currentPage").html("第"+data.page+"页");
				$("#prevPage").html("<a href='"+(data.page==1?"#":BASE_PATH+"/"+(Number(data.page)-1)+"/search.html?title="+title)+"'>上一页</a>");
				$("#nextPage").html("<a href='"+(data.page==data.pageCount?"#":BASE_PATH+"/"+(Number(data.page)+1)+"/search.html?title="+title)+"'>下一页</a>");
				if(data.page==1){
					//第一页
					$("#prevPage").attr("disabled",true);
					$("#nextPage").removeAttr("disabled");
				}else if(data.page==data.pageCount){
					//最后一页
					$("#nextPage").attr("disabled",true);
					$("#prevPage").removeAttr("disabled");
				}else{
					$("#prevPage").removeAttr("disabled");
					$("#nextPage").removeAttr("disabled");
				}
			}
		}
	});
}

function createArticle(data){
	var html = "";
	if(data){
		if(data.data){
			for(var i =0 ; i < data['data'].length;i++){
				if(i == data['data'].length-1){
					html+="<div class='row title-conatiner-end'> <div class='col-md-3 col-sm-12 title-img hidden-xs'> <a href='"+BASE_PATH+"/article/"+data['data'][i].id+".html'><img src='"+BASE_PATH+"/"+data['data'][i].thumbnail+"'></a> </div> <div class='col-md-9 col-sm-12  title-content'> <ul> <a href='"+BASE_PATH+"/article/"+data['data'][i].id+".html'><h3>"+cutWords(26,data['data'][i].title)+"</h3></a> <p>"+cutWords(150,data['data'][i].description)+"</p> <p class='title-info'> <span class='tag'>"+createTag(data['data'][i].tag,data.tag)+"</span> <span class='time'>"+formateDate(data['data'][i].updateTime)+"</span> <span class='comment'></span> <span class='read-count'>阅读("+data['data'][i].readCount+")</span> </p> </ul> </div> </div>";
				}else{
					html+="<div class='row title-conatiner'> <div class='col-md-3 col-sm-12 title-img hidden-xs'> <a href='"+BASE_PATH+"/article/"+data['data'][i].id+".html'><img src='"+BASE_PATH+"/"+data['data'][i].thumbnail+"'></a> </div> <div class='col-md-9 col-sm-12  title-content'> <ul> <a href='"+BASE_PATH+"/article/"+data['data'][i].id+".html'><h3>"+cutWords(26,data['data'][i].title)+"</h3></a> <p>"+cutWords(150,data['data'][i].description)+"</p> <p class='title-info'> <span class='tag'>"+createTag(data['data'][i].tag,data.tag)+"</span> <span class='time'>"+formateDate(data['data'][i].updateTime)+"</span> <span class='comment'></span> <span class='read-count'>阅读("+data['data'][i].readCount+")</span> </p> </ul> </div> </div>";
				}
			}
		}
	}
	$("#article").html(html);
}
